package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.service.IAdminService;
import edu.whut.mall.admin.shiro.realm.MyRealm;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.LoginVo;
import edu.whut.mall.common.util.IpUtil;
import edu.whut.mall.common.api.ResponseMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-01 20.14
 */
@RequiredArgsConstructor
@RequestMapping("/login")
@RestController
@Api(tags = {"登录管理"}, description = "登陆管理器", produces = MediaType.APPLICATION_JSON_VALUE)
public class LoginController {

    private final IAdminService adminService;

    @ApiOperation(value = "测试角色权限接口")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Map<String,Object> add() {
        ResponseMap map = ResponseMap.getInstance();
        return map.putSuccess("进入角色测试页面成功");
    }


    @ApiOperation(value = "测试资源权限接口")
    @RequestMapping(value = "/resource", method = RequestMethod.GET)
    public Map<String,Object> resource() {
        ResponseMap map = ResponseMap.getInstance();
        return map.putSuccess("进入权限测试页面成功");
    }


    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String,Object> login(HttpServletRequest httpServletRequest, String username, String password) {
        //前后端分离项目应该返回loginVo，方便测试返回的string，好跳转页面
        ResponseMap map = ResponseMap.getInstance();
        LoginVo loginVo = adminService.loginAdmin(IpUtil.getIpAddr(httpServletRequest), username, password);
        if (loginVo.getKey() == -1) {
            return map.putFailure("用户名错误", 0);
        }
        if (loginVo.getKey() == -2) {
            return map.putFailure("密码错误", 0);
        }
        return map.putValue(loginVo, "登陆成功");
    }

    //    @RequestMapping(value = "/logout")
    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Map<String,Object> logout() {
        ResponseMap map = ResponseMap.getInstance();
//    public CommonResult logout() {
        ShiroUtil.getSubject().logout();
//        return "login";
        return map.putSuccess("退出成功");
    }

    @ApiOperation(value = "清除用户缓存")
    @RequestMapping(value = "/clear", method = RequestMethod.GET)
    public Map<String,Object> clear() {
        ResponseMap map = ResponseMap.getInstance();
        MyRealm myRealm = new MyRealm();
        myRealm.clearCache();
        return map.putSuccess("清除用户缓存");
    }

}
